package mybatis.test;

import mybatis.dao.StudentDao;
import mybatis.pojo.Student;
import mybatis.pojo.Student3;
import mybatis.util.MybatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Author 汤舒青
 * @Version 1.0
 */


public class TestStudent {
    public static void main(String[] args) {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
            //SQLsession ==Connection
            SqlSession sqlSession = build.openSession();
            //>>>Connection connection = sqlSession.getConnection();
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            System.out.println(mapper.getClass().getName());
            System.out.println(Arrays.toString(mapper.getClass().getInterfaces()));
            List<Student> all = mapper.findALL();
            all.forEach(System.out::println);
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查
     */
    @Test
    public void findAll(){
        SqlSession s = MybatisUtil.get(true);
        StudentDao mapper = s.getMapper(StudentDao.class);
        List<Student> list = mapper.findALL();
        list.forEach(System.out::println);
        MybatisUtil.close(s);
    }
    /**
     * 增
     */
@Test
    public void addStudent(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
       // Student student = new Student("泽丽", "女", "113333252", "589662", "55465", 5, "2022-06-06", "合肥", "2019-04-15", "安庆", "123456", "3402020");
    Student student = new Student();
    student.setSname("维克托");
    student.setGradeid(2);
    student.setEnterdate(new Date());
    int add = mapper.add(student);
        s.commit();
        System.out.println(add==1?"增加成功":"增加失败");
        MybatisUtil.close(s);
    }
    @Test
    public void findLimit(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        List<Student> list = mapper.findAllLimit(3, 10);
        list.forEach(System.out::println);
        MybatisUtil.close(s);
    }
    /**
     * 删
     */
    @Test
    public void deleteStu(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        int i = mapper.delete(2);
        s.commit();
        System.out.println(i==1?"删除成功":"删除失败");
        MybatisUtil.close(s);
    }
    /**
     * 改
     */
    @Test
    public void updateStu(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
       // Student student = new Student(3l,"金克斯", "女", "119633252", "685662", "966565", 5, "2021-05-30", "合肥", "2019-04-15", "峡谷", "134456", "3402020");
        Student student = new Student();
        student.setStuno(3l);
        student.setSname("加里奥");
        student.setSex("妖");
        student.setTelephone("18715252525");
        student.setWechat("5252565");
        int i = mapper.update(student);
        System.out.println(i>0?"更新成功":"更新失败");
        MybatisUtil.close(s);

    }
    /**
     * 求学生总数
     */
    @Test
    public void find(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        int total = mapper.findTotal();
        int total1 = mapper.findTotal();
        System.out.println(total);
        System.out.println(total1);
        MybatisUtil.close(s);
    }
    //    实现按照 按照 微信号,qq号,姓名,模糊查找学生的功能，加上分页
    @Test
    public void search(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        List<Student> list = mapper.search("1", "1", "王", 1, 10);
        list.forEach(System.out::println);
        MybatisUtil.close(s);
    }
    @Test
    public void search2(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        ArrayList<String> list = new ArrayList<>();
        list.add("合肥");
        list.add("阜阳");
        List<Student> students = mapper.search3(list);
        for (Student student : students) {
            System.out.println(student);
        }
        MybatisUtil.close(s);
    }
    @Test
    public void search3(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        List<Student> students = mapper.search4(2);
        students.forEach(System.out::println);
        MybatisUtil.close(s);
    }
    @Test
    public void seatch4(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);

        List<Student> list = mapper.search6(2, null, null, 1, 1, 5);
        for (Student student1 : list) {
            System.out.println(student1);
            MybatisUtil.close(s);
        }
    }
    @Test
    public void seatch5(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        List<Student3> list = mapper.search7();
        for (Student3 student3 : list) {
            System.out.println(student3);
        }
        MybatisUtil.close(s);
    }
    @Test
    public void total(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        int i = mapper.searchTotal(2, null, null, java.sql.Date.valueOf("1900-05-20"), java.sql.Date.valueOf("2020-04-06"));
        System.out.println("人数为"+i);
        MybatisUtil.close(s);
    }
    @Test
    public void findAllByLimit(){
        SqlSession s = MybatisUtil.get();
        StudentDao mapper = s.getMapper(StudentDao.class);
        HashMap<String, Object> map = new HashMap<>();
        map.put("sname","王");
        map.put("start", java.sql.Date.valueOf("1900-05-20"));
        map.put("end", java.sql.Date.valueOf("2020-04-06"));
        map.put("pageNo",1);
        map.put("pageSize",5);
        List<Student> list = mapper.stuByLimit(map);
        int i = mapper.searchAll(map);

        list.forEach(System.out::println);
        System.out.println("查询到的总人数为"+i);
        MybatisUtil.close(s);

    }
}
